package org.linlinjava.litemall.db.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.apache.ibatis.type.IntegerTypeHandler;
import org.linlinjava.litemall.db.handler.IntegerArrayJsonTypeHandler;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * litemall优惠券
 *
 * @author yliyun
 * @date 2022/10/12
 */
@Data
@TableName("litemall_coupon")
public class LitemallCoupon {
    /**
     * 被删除
     */
    public static final Boolean IS_DELETED = Deleted.IS_DELETED.value();

    /**
     * 不删除
     */
    public static final Boolean NOT_DELETED = Deleted.NOT_DELETED.value();

    /**
     * id
     */
    @TableId
    private Integer id;

    /**
     * 名字
     */
    private String name;

    /**
     * desc
     */
    private String desc;

    /**
     * 标签
     */
    private String tag;

    /**
     * 总计
     */
    private Integer total;

    /**
     * 折扣
     */
    private BigDecimal discount;

    /**
     * 折扣类型
     */
    private Byte discountType;

    /**
     * 折现率
     */
    private Byte discountRate;

    /**
     * 最小值
     */
    private BigDecimal min;

    /**
     * 限制
     */
    private Short limit;

    /**
     * 类型
     */
    private Short type;

    /**
     * 用户级
     */
    @TableField(typeHandler = IntegerTypeHandler.class)
    private Integer[] userLevel;

    /**
     * 状态
     */
    private Short status;

    /**
     * 商品类型
     */
    private Short goodsType;

    /**
     * 货物价值
     */
    @TableField(typeHandler = IntegerArrayJsonTypeHandler.class)
    private Integer[] goodsValue;

    /**
     * 代码
     */
    private String code;

    /**
     * 条形码
     */
    private String barCode;

    /**
     * 时间类型
     */
    private Short timeType;

    /**
     * 天
     */
    private Short days;

    /**
     * 开始时间
     */
    private LocalDateTime startTime;

    /**
     * 结束时间
     */
    private LocalDateTime endTime;

    /**
     * 促销仅
     */
    private Boolean promotionOnly;

    /**
     * 使用指令
     */
    private String useInstruction;

    /**
     * 添加时间
     */
    private LocalDateTime addTime;

    /**
     * 更新时间
     */
    private LocalDateTime updateTime;

    /**
     * 删除
     */
    private Boolean deleted;

    /**
     * 范围
     */
    @TableField(typeHandler = IntegerTypeHandler.class)
    private Integer[] ranges;

    /**
     * 使用限制
     */
    private Short useLimit;

    /**
     * 使用限制num
     */
    private Integer useLimitNum;

    /**
     * 级别成员类型
     */
    @TableField(typeHandler = IntegerTypeHandler.class)
    private Integer[] levelMemberType;

    /**
     * 钱成员类型
     */
    @TableField(typeHandler = IntegerTypeHandler.class)
    private Integer[] moneyMemberType;

    /**
     * 地区
     */
    @TableField(typeHandler = IntegerTypeHandler.class)
    private Integer[] regions;

    /**
     * 承担损失
     */
    private Short lossBearing;

    /**
     * 区域限制
     */
    private Short areaLimit;

    /**
     * 照片url
     */
    private String pictureUrl;

    /**
     * 盈余
     */
    private Integer surplus;

    /**
     * 分发状态
     */
    private Short distributeStatus;

    /**
     * 生日福利券（0 No | 1 YES）
     */
    private Integer isBirthdayUse;

    /**
     * 有num
     */
    @TableField(exist = false)
    private Integer haveNum;


    public enum Deleted {
        NOT_DELETED(new Boolean("0"), "未删除"),
        IS_DELETED(new Boolean("1"), "已删除");

        private final Boolean value;

        private final String name;

        Deleted(Boolean value, String name) {
            this.value = value;
            this.name = name;
        }

        public Boolean getValue() {
            return this.value;
        }

        public Boolean value() {
            return this.value;
        }

        public String getName() {
            return this.name;
        }
    }

    public enum Column {
        id("id", "id", "INTEGER", false),
        name("name", "name", "VARCHAR", true),
        desc("desc", "desc", "VARCHAR", true),
        tag("tag", "tag", "VARCHAR", false),
        total("total", "total", "INTEGER", false),
        discount("discount", "discount", "DECIMAL", false),
        discountType("discount_type", "discountType", "TINYINT", false),
        discountRate("discount_rate", "discountRate", "TINYINT", false),
        min("min", "min", "DECIMAL", true),
        limit("limit", "limit", "SMALLINT", true),
        type("type", "type", "SMALLINT", true),
        userLevel("user_level", "userLevel", "VARCHAR", false),
        status("status", "status", "SMALLINT", true),
        goodsType("goods_type", "goodsType", "SMALLINT", false),
        goodsValue("goods_value", "goodsValue", "VARCHAR", false),
        code("code", "code", "VARCHAR", false),
        barCode("bar_code", "barCode", "VARCHAR", false),
        timeType("time_type", "timeType", "SMALLINT", false),
        days("days", "days", "SMALLINT", true),
        startTime("start_time", "startTime", "TIMESTAMP", false),
        endTime("end_time", "endTime", "TIMESTAMP", false),
        promotionOnly("promotion_only", "promotionOnly", "BIT", false),
        useInstruction("use_instruction", "useInstruction", "VARCHAR", false),
        addTime("add_time", "addTime", "TIMESTAMP", false),
        updateTime("update_time", "updateTime", "TIMESTAMP", false),
        deleted("deleted", "deleted", "BIT", false),
        ranges("ranges", "ranges", "SMALLINT", false),
        useLimit("use_limit", "useLimit", "SMALLINT", false),
        useLimitNum("use_limit_num", "useLimitNum", "INTEGER", false),
        levelMemberType("level_member_type", "levelMemberType", "VARCHAR", false),
        moneyMemberType("money_member_type", "moneyMemberType", "VARCHAR", false),
        regions("regions", "regions", "VARCHAR", false),
        lossBearing("loss_bearing", "lossBearing", "SMALLINT", false),
        areaLimit("area_limit", "areaLimit", "SMALLINT", false),
        pictureUrl("picture_url", "pictureUrl", "VARCHAR", false),
        surplus("surplus", "surplus", "INTEGER", false),
        distributeStatus("distribute_status", "distributeStatus", "SMALLINT", false);

        private static final String BEGINNING_DELIMITER = "`";

        private static final String ENDING_DELIMITER = "`";

        private final String column;

        private final boolean isColumnNameDelimited;

        private final String javaProperty;

        private final String jdbcType;

        public String value() {
            return this.column;
        }

        public String getValue() {
            return this.column;
        }

        public String getJavaProperty() {
            return this.javaProperty;
        }

        public String getJdbcType() {
            return this.jdbcType;
        }

        Column(String column, String javaProperty, String jdbcType, boolean isColumnNameDelimited) {
            this.column = column;
            this.javaProperty = javaProperty;
            this.jdbcType = jdbcType;
            this.isColumnNameDelimited = isColumnNameDelimited;
        }

        public String desc() {
            return this.getEscapedColumnName() + " DESC";
        }

        public String asc() {
            return this.getEscapedColumnName() + " ASC";
        }

        public static Column[] excludes(Column ... excludes) {
            ArrayList<Column> columns = new ArrayList<>(Arrays.asList(Column.values()));
            if (excludes != null && excludes.length > 0) {
                columns.removeAll(new ArrayList<>(Arrays.asList(excludes)));
            }
            return columns.toArray(new Column[]{});
        }

        public String getEscapedColumnName() {
            if (this.isColumnNameDelimited) {
                return new StringBuilder().append(BEGINNING_DELIMITER).append(this.column).append(ENDING_DELIMITER).toString();
            } else {
                return this.column;
            }
        }

        public String getAliasedEscapedColumnName() {
            return this.getEscapedColumnName();
        }
    }
}
